<template>
  <div class="app-container calendar-list-container">
    <el-form class="small-space" :model="reqData" label-position="right" label-width="90px" :inline="true">

      <!-- 头信息 -->
      <create-req-head :reqData="reqData"></create-req-head>

      <!-- 明细信息 -->
      <create-req-detail :reqData="reqData"></create-req-detail>

      <el-button
        type="primary"
        size="large"
        style="margin: 10px auto;width: 100%;"
        @click.native="save">保存信息</el-button>

    </el-form>

  </div>
</template>

<script>
  import { format } from '@/utils/dateFmt'
  import createReqHead from './createReq_head.vue'
  import createReqDetail from './createReq_detail.vue'
  import {
    modifyRequest,
    requesLineList
  } from '@/api/purchase/request'

  export default {
    components: {
      createReqHead,
      createReqDetail
    },
    name: 'createCompute',
    data() {
      return {
        reqData:{
          client_type:1,
          company_id:'',
          company_name:'',
          head_info:{
            dep_id:null,
            dep_name:null,
            reason:null,
            request_date:null,
            request_id:null,
            request_man:null,
            request_man_name:null,
            request_no:null,
            line_list:[]
          }
        },
        req_lineList:{
          client_type:1,
          request_id:null
        }

      }
    },
    mounted() {

    },
    methods: {
      save(){
        //将日期转为指定格式的字符串
        this.reqData.head_info.request_date = format(new Date(this.reqData.head_info.request_date),'yyyy-MM-dd')

        modifyRequest(this.reqData)
          .then(res => {
            this.reqData.head_info.request_id = res.request_id
            this.reqData.head_info.request_no = res.request_no
            this.req_lineList.request_id = res.request_id
            requesLineList(this.req_lineList)  //保存成功后再取一下明细列表.主要是为了行id
              .then(res => {
                this.reqData.head_info.line_list = res.line_list
              })
              .catch(err => {
                console.log(err)
                this.$message.error('获取明细失败!')
              })

            this.$message({
              type:'success',
              message:'保存成功!'
            })
          })
          .catch(err => {
            console.log(err)
            this.$message.error('保存失败!')
          })
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
